package sz.com.postoffice.jxyzwgcase.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import sz.com.postoffice.jxyzwgcase.core.BaseResponseEntity;
import sz.com.postoffice.jxyzwgcase.core.ResultCode;
import sz.com.postoffice.jxyzwgcase.service.OneClickService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Api(value = "NEW一键响应", tags = "{NEW一键响应}")
@RequestMapping(value = "/newOneClick")
public class OneClickController {
    private static Logger logger = LoggerFactory.getLogger(OneClickResponseController.class);
    @Resource
    private OneClickService oneClickService;

    @ApiOperation(value = "查询超时节点详情", notes = "查询超时节点详情")
    @RequestMapping(value = "/findProcessingTimeoutDetail", method = RequestMethod.POST, produces = "application/json")
    public BaseResponseEntity findProcessingTimeoutDetail(@RequestBody Map map) {
        return BaseResponseEntity.success(0, "查询成功.", oneClickService.findProcessingTimeoutDetail(map));
    }

    @ApiOperation(value = "查询超时节点统计", notes = "查询超时节点统计")
    @RequestMapping(value = "/findProcessingTimeoutSummary", method = RequestMethod.POST, produces = "application/json")
    public BaseResponseEntity findProcessingTimeoutSummary(@RequestBody Map map) {
        List<Map> processingTimeout = oneClickService.findProcessingTimeoutSummary(map);
        return BaseResponseEntity.success(0, "查询成功.", processingTimeout);
    }

    @ApiOperation(value = "查询超时节点导出", notes = "查询超时节点导出")
    @RequestMapping(value = "/findProcessingTimeoutExport", method = RequestMethod.GET, produces = "application/json")
    public BaseResponseEntity findProcessingTimeoutSummaryExport(HttpServletResponse response, @RequestParam(required = false) Map map) throws IOException {
        if (map.containsKey("proc_edf_ID")) {
            String[] split = map.get("proc_edf_ID").toString().split(",");
            List<String> list = CollectionUtils.arrayToList(split);
            map.put("proc_edf_ID", list);
        }
        if (map.containsKey("exportType")) {
            XSSFWorkbook excel = null;
            if (("summary").equals(map.get("exportType"))) {
                excel = oneClickService.findProcessingTimeoutSummaryToExcel(map);
            } else if (("detail").equals(map.get("exportType"))) {
                excel = oneClickService.findProcessingTimeoutDetailToExcel(map);
            } else {
                return BaseResponseEntity.failure(ResultCode.FAIL, "exportType参数错误");
            }
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download");
            String fileName = excel.getSheetName(0) + ".xlsx";
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            OutputStream out = response.getOutputStream();
            excel.write(out);
            out.close();
            excel.close();
        } else {
            return BaseResponseEntity.failure(ResultCode.FAIL, "exportType未填写");
        }

        return null;
    }

}
